package com.demo.widget;

import com.demo.widget.provider.StackLayoutMangerProvider;
import com.meis.widget.spiderweb.SpiderWebView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

public class MainAbility extends BaseAbility {
    private static final int REQUEST_CODE = 0X01;
    private ListContainer mListContainer;
    private final String[] mTitles = {
            "MEI控件",
            "郭鸿控件",
            "关于我",
    };
    private final Class[] mJumpAbilities = {
            com.demo.widget.meis.MainAbility.class,
            com.demo.widget.guhong.MainAbility.class,
            AboutAbility.class};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        mListContainer.setItemProvider(new StackLayoutMangerProvider(this, mTitles));
        mListContainer.setItemClickedListener((listContainer, component, i, l) -> {
            Intent intent1 = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAbilityName(mJumpAbilities[i])
                    .withBundleName(getBundleName())
                    .build();
            intent1.setOperation(operation);
            startAbility(intent1);
        });
        initPromission();
    }

    private void initPromission(){
        if (verifyCallingOrSelfPermission(SystemPermission.READ_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(new String[]{SystemPermission.READ_USER_STORAGE}, REQUEST_CODE);
        }
    }
}
